# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


from flask import Flask
from flask import redirect
from flask import url_for
from flask_sqlalchemy import SQLAlchemy
from flask_basicauth import BasicAuth
from flask_admin.contrib.sqla import ModelView
from flask_admin import BaseView, expose,Admin

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:mysql@127.0.0.1:3306/zczh_web"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_ECHO'] = False
app.config['BASIC_AUTH_USERNAME']='123'
app.config ['BASIC_AUTH_PASSWORD']='123'
app.config['SERCRET_KEY']='asdasdasd'

db=SQLAlchemy(app)


class Role(db.Model):
    __tablename__ = "roles"
    id = db.Column(db.Integer, primary_key=True)
    name=db.Column(db.String(64),nullable=False,unique=True)
    us = db.relationship('User', backref='role')

    def __repr__(self):
        return 'Role:%s' % self.name

class User(db.Model):
    __tablename__='users'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(64),nullable=False)
    pwd=db.Column(db.String(64))
    roles_id= db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return 'User:%s' % self.name

class Content(db.Model):
    __tablename__='contents'
    id = db.Column(db.Integer,default=0)
    content = db.Column(db.BLOB,nullable=False)
    md5 = db.Column(db.String(32),primary_key=True)
    list_=db.relationship('List_',backref='content')
    def __repr__(self):
        return 'Content:%s' % self.content

class Content_admin(db.Model):
    __tablename__='contents_admin'
    id = db.Column(db.Integer,primary_key=True)
    content_admin = db.Column(db.BLOB,nullable=False)
    list_admin=db.relationship('List_',backref='content_admin')
    def __repr__(self):
        return 'Content_admin:%s' % self.content_admin

class Code(db.Model):
    __tablename__='codes'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    list_=db.relationship('List_',backref='code')
    is_show=db.Column(db.SmallInteger,default=1)
    def __repr__(self):
        return 'Code:%s' % self.name

class List_(db.Model):
    __tablename__='list_s'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(64), nullable=False)
    code_id = db.Column(db.Integer, db.ForeignKey('codes.id'))
    md5 = db.Column(db.String(32),db.ForeignKey('contents.md5'),default=None)
    content_id = db.Column(db.Integer, db.ForeignKey('contents_admin.id'),default=0)
    update_time = db.Column(db.Date)
    abstract=db.Column(db.String(256))
    mark=db.Column(db.SmallInteger)
    def __repr__(self):
        return 'List_:%s' % self.title


basic_auth = BasicAuth(app)
@app.route('/admin')
@basic_auth.required
def secret_view():
    return redirect(url_for('admin.index'))
#修该ModelView


class A(ModelView):
    can_view_details = True
    create_modal = True
    edit_modal = True
    column_filters = ['code','mark']
    column_editable_list = ['code', 'mark']
    form_choices = {
        'mark': [
            ('1','1'),
        ]
    }
class BlogModelView(ModelView):
    can_view_details = True
    create_modal = True
    edit_modal = True

class AnalyticsView(BaseView):
    @expose('/')
    def index(self):
        return self.render('admin/analytics_index.html')



admin = Admin(app, name='主菜单', template_mode='bootstrap3')
admin.add_view(BlogModelView(User, db.session,name='用户管理'))
admin.add_view(BlogModelView(Role, db.session,name='角色管理'))
admin.add_view(A(List_, db.session,name='列表展示页面'))
admin.add_view(BlogModelView(Code, db.session,name='展示类别'))
admin.add_view(BlogModelView(Content, db.session,name='爬虫详情页面展示'))
admin.add_view(BlogModelView(Content_admin, db.session,name='后台管理详情页面展示'))



@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run(debug=True,host='192.168.1.103')
